import Upload from '@/components/common/Upload.vue';
export default {
    name: 'CompletePictures',
    components: {
        Upload
    },
    inject: ['CheckApplication'],
    props: ['data'],
    data() {
        return {
            builtImg: {},
            uploadFileList: [],
            uploadHttp: '/api/attachment/uploadFiles',
            fileType: [
                { label: '门楣整体', type: '1' },
                { label: '室内天花', type: '2' },
                { label: '室外门口', type: '3' },
                { label: '室内地面', type: '4' },
                { label: '室内整体', type: '5' },
                { label: '室内墙壁', type: '6' },
                { label: '卫生间', type: '7' },
                { label: '拆除项', type: '8' },
                { label: '其他', type: '9' }
            ],
            downPath: process.env.VUE_APP_BASE_URL + 'attachment/download?fileId=',
            builtImgs: []
        };
    },
    computed: {
        fileList() {
            return this.fileType.map(v => {
                const fileList = this.data.fileList;
                const findItem = fileList?.find(file => file.fileType === v.fileType);
                const rec = findItem?.reconnaissanceFiles;
                return {
                    workOrderId: this.data.workOrderId,
                    fileType: v.type,
                    reconnaissanceFiles: rec || ''
                };
            });
        },
        srcUrl() {
            const ret = this.fileType.map(v => {
                const fileList = this.data.fileList || [];
                const files = fileList.find(file => file.fileType === v.type) || '';
                const fileIds = (files && files.reconnaissanceFiles.split(',')) || [''];
                return fileIds.map(id => this.downPath + id);
            });
            return ret;
        }
    },
    watch: {
        'data.fileList': {
            immediate: true,
            async handler() {
                if (!this.data.fileList) return [];
                //   fileType=11 施工图
                const type = this.data.fileList.find(v => v.fileType === '11');
                const fileIds = type && type.reconnaissanceFiles;
                if (!fileIds) return [];
                const response = await this.$httpExt().get('/api/attachment/getAttachment', {
                    fileIds: fileIds,
                    _: new Date().getTime()
                });
                if (!response.data) return [];
                this.builtImgs = response.data.map(({ attachmentId, attachmentName }) => ({
                    url: this.downPath + attachmentId,
                    name: attachmentName
                }));
            }
        }
    },
    methods: {
        handleBuiltImg(item) {
            location.href = item.url;
        },
        handleRemove(file, fileList) {
            this.builtImg = {
                fileType: '11',
                workOrderNo: this.data.workOrderNo,
                reconnaissanceFiles: fileList.map(v => v.response).join(',') || ''
            };
        },
        handleChange(response, file, fileList) {
            this.builtImg = {
                fileType: '11',
                workOrderNo: this.data.workOrderNo,
                reconnaissanceFiles: fileList.map(v => v.response).join(',') || ''
            };
        },
        getToken() {
            let result = sessionStorage.getItem('token');
            return result;
        }, //显示附件
        showFiles(fileIds) {
            if (fileIds) {
                //获取附件名称
                this.$httpExt()
                    .get('/api/attachment/getAttachment', {
                        fileIds: fileIds,
                        _: new Date().getTime()
                    })
                    .then(
                        response => {
                            let result = response.result;
                            if (undefined !== result && result !== null) {
                                for (let i = 0; i < result.length; i++) {
                                    let attachmentName = result[i].attachmentName;
                                    let attachmentId = result[i].attachmentId;
                                    let fileArray = fileIds.split(',');
                                    for (let j = 0; j < fileArray.length; j++) {
                                        if (fileArray[j] === attachmentId) {
                                            let fileUrl =
                                                process.env.VUE_APP_BASE_URL +
                                                'attachment/download?fileId=' +
                                                fileArray[j];
                                            let fileArr = {
                                                name: attachmentName,
                                                url: fileUrl,
                                                fileId: fileArray[j]
                                            };
                                            this.uploadFileList.push(fileArr);
                                            break;
                                        }
                                    }
                                }
                            }
                        },
                        () => {
                            this.$message({
                                message: '附件服务器请求失败，请重试！',
                                type: 'warning'
                            });
                        }
                    );
            }
        }
    }
};
